package ringstarproject;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.graphstream.graph.ElementNotFoundException;
import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.MultiGraph;
import org.graphstream.stream.GraphParseException;


public class TSPtoDGS {

        private static String pathDGS = "";
        private String cssCycle= "";
        
	public TSPtoDGS()
	{
           
        }

        public void createDGSFile(TSP_data tsp)
        {
                pathDGS = tsp.getPath().replace(".tsp", ".dgs");
                
                //on met try si jamais il y a une exception
                try
                {
                        /**
                         * BufferedWriter a besoin d un FileWriter,
                         * les 2 vont ensemble, on donne comme argument le nom du fichier
                         * true signifie qu on ajoute dans le fichier (append), on ne marque pas par dessus

                         */
                        FileWriter fw = new FileWriter(pathDGS, false);

                        // le BufferedWriter output auquel on donne comme argument le FileWriter fw cree juste au dessus
                        BufferedWriter output = new BufferedWriter(fw);

                        output.write("DGS003\n");
                        output.write("null 0 0\n");
                        for(int i = 0 ; i < tsp.getX().size(); i++)
                        {
                            output.write("an " + "\"" +(i+1)+"\"" +" x:"+ tsp.getX().get(i) + " y:"+tsp.getY().get(i)+"\n" );
                        }

                        for(int i = 0 ; i < tsp.getSommetsCycle().size(); i++)
                        {
                            if(i == tsp.getSommetsCycle().size()-1)
                            {
                                cssCycle += "edge#\""+tsp.getSommetsCycle().get(i)+"_"+ tsp.getSommetsCycle().get(0) + "\" { fill-color: red; size:3px; }";
                                output.write("ae " + "\"" +tsp.getSommetsCycle().get(i)+"_"+tsp.getSommetsCycle().get(0) +"\" \""+ tsp.getSommetsCycle().get(i) + "\" \""+ tsp.getSommetsCycle().get(0) + "\" length:"+ tsp.getDistance()[tsp.getSommetsCycle().get(i)-1][tsp.getSommetsCycle().get(0)-1]+"\n" );
                            } else{
                                cssCycle += "edge#\""+tsp.getSommetsCycle().get(i)+"_"+ tsp.getSommetsCycle().get(i+1) + "\" { fill-color: red;  size:3px;}";
                                output.write("ae " + "\"" +tsp.getSommetsCycle().get(i)+"_"+tsp.getSommetsCycle().get(i+1) +"\" \""+ tsp.getSommetsCycle().get(i) + "\" \""+ tsp.getSommetsCycle().get(i+1) + "\" length:"+ tsp.getDistance()[tsp.getSommetsCycle().get(i)-1][tsp.getSommetsCycle().get(i+1)-1]+"\n" );
                            }
                        }
                        for(int i = 0 ; i < tsp.getSommetsRestants().size(); i++)
                        {                            
                            int pps = tsp.plusProcheSommetCycle(tsp.getSommetsRestants().get(i));                       
                            output.write("ae " + "\"" +tsp.getSommetsRestants().get(i)+"_"+ pps +"\" \""+ tsp.getSommetsRestants().get(i) + "\" \""+ pps + "\" length:"+ tsp.getDistance()[tsp.getSommetsRestants().get(i)-1][pps-1]+"\n" );
                        }

                        output.flush();
                        //ensuite flush envoie dans le fichier, ne pas oublier cette methode pour le BufferedWriter

                        output.close();

                }
                catch(IOException ioe){
                        System.out.print("Erreur : ");
                        ioe.printStackTrace();
                }

                visualizeGraph();
    }

    public void visualizeGraph()
    {
        try {
            //TSP_data tsp = new TSP_data();
            Graph graph = new MultiGraph("RSP");
            graph.read(pathDGS);
            graph.addAttribute("ui.quality");
            graph.addAttribute("ui.antialias");
            graph.addAttribute("ui.stylesheet", "graph {  }"
                                              + "node {  fill-color:rgb(125,125,125);}"
                                              + cssCycle 
                    + "");
            graph.display(false);

            
        } catch (IOException ex) {
            Logger.getLogger(TSPtoDGS.class.getName()).log(Level.SEVERE, null, ex);
        } catch (GraphParseException ex) {
            Logger.getLogger(TSPtoDGS.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ElementNotFoundException ex) {
            Logger.getLogger(TSPtoDGS.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
